
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Apps.Models;
using Apps.Common;
using Microsoft.Practices.Unity;
using System.Transactions;
using Apps.BLL.Core;
using Apps.Locale;
using LinqToExcel;
using System.IO;
using System.Text;
using Apps.IDAL.MIS;
using Apps.Models.MIS;
using Apps.IBLL.MIS;
namespace Apps.BLL.MIS
{
	public partial class MIS_Article_AlbumsBLL: Virtual_MIS_Article_AlbumsBLL,IMIS_Article_AlbumsBLL
	{
        

	}
	public class Virtual_MIS_Article_AlbumsBLL
	{
        [Dependency]
        public IMIS_Article_AlbumsRepository m_Rep { get; set; }

		public virtual List<MIS_Article_AlbumsModel> GetList(ref GridPager pager, string queryStr)
        {

            IQueryable<MIS_Article_Albums> queryData = null;
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(
								a=>a.Id.Contains(queryStr)
								|| a.ArticleId.Contains(queryStr)
								|| a.BigImg.Contains(queryStr)
								|| a.SmallImg.Contains(queryStr)
								|| a.Remark.Contains(queryStr)
								);
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

		public virtual List<MIS_Article_AlbumsModel> GetListByParentId(ref GridPager pager, string queryStr,object parentId)
        {
			return new List<MIS_Article_AlbumsModel>();
		}

        public virtual List<MIS_Article_AlbumsModel> CreateModelList(ref IQueryable<MIS_Article_Albums> queryData)
        {

            List<MIS_Article_AlbumsModel> modelList = (from r in queryData
                                              select new MIS_Article_AlbumsModel
                                              {
													Id = r.Id,
													ArticleId = r.ArticleId,
													BigImg = r.BigImg,
													SmallImg = r.SmallImg,
													Remark = r.Remark,
          
                                              }).ToList();

            return modelList;
        }

        public virtual bool Create(ref ValidationErrors errors, MIS_Article_AlbumsModel model)
        {
            try
            {
                MIS_Article_Albums entity = m_Rep.GetById(model.Id);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                entity = new MIS_Article_Albums();
               				entity.Id = model.Id;
				entity.ArticleId = model.ArticleId;
				entity.BigImg = model.BigImg;
				entity.SmallImg = model.SmallImg;
				entity.Remark = model.Remark;
  

                if (m_Rep.Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }



         public virtual bool Delete(ref ValidationErrors errors, object id)
        {
            try
            {
                if (m_Rep.Delete(id) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Delete(ref ValidationErrors errors, object[] deleteCollection)
        {
            try
            {
                if (deleteCollection != null)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        if (m_Rep.Delete(deleteCollection) == deleteCollection.Length)
                        {
                            transactionScope.Complete();
                            return true;
                        }
                        else
                        {
                            Transaction.Current.Rollback();
                            return false;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
       

        public virtual bool Edit(ref ValidationErrors errors, MIS_Article_AlbumsModel model)
        {
            try
            {
                MIS_Article_Albums entity = m_Rep.GetById(model.Id);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }
                              				entity.Id = model.Id;
				entity.ArticleId = model.ArticleId;
				entity.BigImg = model.BigImg;
				entity.SmallImg = model.SmallImg;
				entity.Remark = model.Remark;
 


                if (m_Rep.Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return false;
                }

            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

      

        public virtual MIS_Article_AlbumsModel GetById(object id)
        {
            if (IsExists(id))
            {
                MIS_Article_Albums entity = m_Rep.GetById(id);
                MIS_Article_AlbumsModel model = new MIS_Article_AlbumsModel();
                              				model.Id = entity.Id;
				model.ArticleId = entity.ArticleId;
				model.BigImg = entity.BigImg;
				model.SmallImg = entity.SmallImg;
				model.Remark = entity.Remark;
 
                return model;
            }
            else
            {
                return null;
            }
        }


		 /// <summary>
        /// 校验Excel数据,这个方法一般用于重写校验逻辑
        /// </summary>
        public virtual bool CheckImportData(string fileName, List<MIS_Article_AlbumsModel> list,ref ValidationErrors errors )
        {
          
            var targetFile = new FileInfo(fileName);

            if (!targetFile.Exists)
            {

                errors.Add("导入的数据文件不存在");
                return false;
            }

            var excelFile = new ExcelQueryFactory(fileName);

            //对应列头
			 				 excelFile.AddMapping<MIS_Article_AlbumsModel>(x => x.ArticleId, "ArticleId");
				 excelFile.AddMapping<MIS_Article_AlbumsModel>(x => x.BigImg, "BigImg");
				 excelFile.AddMapping<MIS_Article_AlbumsModel>(x => x.SmallImg, "SmallImg");
				 excelFile.AddMapping<MIS_Article_AlbumsModel>(x => x.Remark, "Remark");
 
            //SheetName
            var excelContent = excelFile.Worksheet<MIS_Article_AlbumsModel>(0);
            int rowIndex = 1;
            //检查数据正确性
            foreach (var row in excelContent)
            {
                var errorMessage = new StringBuilder();
                var entity = new MIS_Article_AlbumsModel();
						 				  entity.Id = row.Id;
				  entity.ArticleId = row.ArticleId;
				  entity.BigImg = row.BigImg;
				  entity.SmallImg = row.SmallImg;
				  entity.Remark = row.Remark;
 
                //=============================================================================
                if (errorMessage.Length > 0)
                {
                    errors.Add(string.Format(
                        "第 {0} 列发现错误：{1}{2}",
                        rowIndex,
                        errorMessage,
                        "<br/>"));
                }
                list.Add(entity);
                rowIndex += 1;
            }
            if (errors.Count > 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public virtual void SaveImportData(IEnumerable<MIS_Article_AlbumsModel> list)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    foreach (var model in list)
                    {
                        MIS_Article_Albums entity = new MIS_Article_Albums();
                       						entity.Id = ResultHelper.NewId;
						entity.ArticleId = model.ArticleId;
						entity.BigImg = model.BigImg;
						entity.SmallImg = model.SmallImg;
						entity.Remark = model.Remark;
 
                        db.MIS_Article_Albums.Add(entity);
                    }
                    db.SaveChanges();
                }
            }
            catch
            {
                throw;
            }
        }


        public virtual bool IsExists(object id)
        {
            return m_Rep.IsExist(id);
        }
		  public void Dispose()
        { 
            
        }

	}
}
